package dto

import (
	"time"
	"wxpay/internal/model"
	"wxpay/pkg/util"
)

type RefundApplyDTO struct {
	ID                 int64
	BrandID            string
	MchID              string
	SubMchID           string
	AppID              string
	SubAppID           string
	OutTradeNo         string
	WxTransID          string
	OutRefundNo        string
	Reason             string
	RefundAmount       RefundAmount
	RefundAccount      string
	RefundID           string
	Channel            string
	UserReceiveAccount string
	Status             string
	SuccessTime        *time.Time
	FundsAccount       string
}

type RefundAmount struct {
	Refund     int64
	Total      int64
	PayerTotal int64
	Currency   string
}

func AdaptRefund(info *RefundApplyDTO) (r model.Refund) {
	amount := model.RefundAmount{
		Refund:     uint(info.RefundAmount.Refund),
		Total:      uint(info.RefundAmount.Total),
		Currency:   info.RefundAmount.Currency,
		PayerTotal: uint(info.RefundAmount.PayerTotal),
	}
	n := util.GetNow()
	r = model.Refund{
		ID:           info.ID,
		BrandID:      info.BrandID,
		MchID:        info.MchID,
		SubMchID:     info.SubMchID,
		AppID:        info.AppID,
		SubAppID:     util.AdaptNullString(info.SubAppID),
		OutTradeNo:   info.OutTradeNo,
		WxTransID:    info.WxTransID,
		OutRefundNo:  info.OutRefundNo,
		Reason:       util.AdaptNullString(info.Reason),
		AmountInfo:   amount.ToJson(),
		RefundID:     util.AdaptNullString(info.RefundID),
		CreateTime:   n,
		UpdateTime:   n,
		FundsAccount: util.AdaptNullString(info.FundsAccount),
	}
	return
}

// 退款通知参数
type RefundNotificationDTO struct {
	// 服务商户号
	SpMchID string `json:"sp_mchid"`
	// 二级商户号
	SubMchID string `json:"sub_mchid"`
	// 商户订单号
	OutTradeNo string `json:"out_trade_no"`
	// 微信支付订单号
	TransactionID string `json:"transaction_id"`
	// 商户退款单号
	OutRefundNo string `json:"out_refund_no"`
	// 微信退款单号
	RefundID string `json:"refund_id"`
	// 退款状态
	RefundStatus string `json:"refund_status"`
	// 退款成功时间
	SuccessTime string `json:"success_time"`
	// 退款入账账户
	UserReceivedAccount string `json:"user_received_account"`
	// 金额信息
	Amount RefundNotifyAmount `json:"amount"`
}

type RefundNotifyAmount struct {
	// 订单金额
	Total uint `json:"total"`
	// 退款金额
	Refund uint `json:"refund"`
	// 用户支付金额
	PayerTotal uint `json:"payer_total"`
	// 用户退款金额
	PayerRefund uint `json:"payer_refund"`
}
